﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zatify.Domain.Data;
using System.Text;

namespace System.Web.Mvc
{
    public static class HtmlTreeViewHelperExtension
    {
        public static MvcHtmlString CategoryTree(this HtmlHelper html, List<Category> categories, bool showSingleSelection, int? excludeCategoryId)
        {
            string htmlOutput = string.Empty;

            if (categories.Count() > 0)
            {
                htmlOutput += "<ul>";
                foreach (var category in categories.OrderBy(c => c.SortOrder))
                {
                    if (excludeCategoryId.HasValue && category.CategoryId == excludeCategoryId.Value)
                        continue;

                    if (category.IsSingleSelection && !showSingleSelection)
                        continue;

                    var isSelectableCategory = (category.ChildCategories.Count() <= 0);

                    htmlOutput += "<li id=\"" + category.CategoryId + "\" data=\"isSelectable: '" + isSelectableCategory + "'\">";
                    htmlOutput += category.CategoryName;
                    htmlOutput += CategoryTree(html, category.ChildCategories.ToList(), showSingleSelection, excludeCategoryId);
                    htmlOutput += "</li>";
                }
                htmlOutput += "</ul>";
            }

            return new MvcHtmlString(htmlOutput);
        }
    }
}